CONST Statement Action Declares symbolic constants for use in place of values. Syntax CONST constantname = expression , - constantname = expression-... Remarks The CONST statement uses the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- constantname A name that follows the same rules as a BASIC variable. You can add a type-declaration character ( %, &, !, #, @, or $) to indicate its type, but this character is not part of the name. expression An expression that consists of literals (such as 1.0), other constants, or any of the arithmetic and logical operators except exponentiation (^). You also can use a single literal string such as "Error on input". You cannot use string concatenation, variables, Argument Description ---------------------------------------------------------------------------- concatenation, variables, user-defined functions, or intrinsic functions--such as SIN or CHR$--in expressions assigned to constants. If you use a type-declaration character in the name, you can omit the character when the name is used, as shown in the following example. CONST MAXDIM% = 250 . . . DIM AccountNames$(MAXDIM) If you omit the type-declaration character, the constant is given a type based on the expression in the CONST statement. Strings always yield a string constant. With numeric expressions, the expression is evaluated and the constant is given the simplest type that can represent the constant. For example, if the expression gives a result that can be represented as an integer, the constant is given an integer type. Note Names of constants are not affected by DEF type statements such as DEFINT. A constant's type is determined either by an explicit type-declaration character or by the type of the expression. Constants must be defined before they are referred to. The following example produces an error because the constant ONE is not defined before it is used to define TWO (constants are defined from left to right). CONST TWO = ONE + ONE, ONE = 1 Note You cannot use ASCII 01 and 02 in string constants if you are going to compile to an executable program. The compiler (BC.EXE) uses ASCII 01 and 02 internally to represent End-of-Statement and End-of-Line, respectively. You can, however, still use 1 and 2 within the QBX environment. Constants declared in a SUB or FUNCTION procedure are local to that procedure. A constant declared outside a procedure is defined throughout the module. You can use constants anywhere that you would use an expression. A common programming practice is to use a statement such as the following (any non-zero value represents "true"). TRUE=-1 Using constants offers several advantages over using variables for constant values. - You only need define constants once for an entire module. - Constants cannot be inadvertently changed. - In stand-alone programs, using constants produces faster and often smaller code than variables. - Constants make programs easier to modify. Example The following example uses the CONST statement to declare symbolic constants for the ASCII values of nonprinting characters such as tab and line feed. Constants also make programs easier to modify. The program counts words, lines, and characters in a text file. A word is any sequence of nonblank characters. DEFINT a-z CONST BLANK = 32, ENDFILE = 26, CR = 13, LF = 10 CONST TABC = 9, YES = -1, NO = 0 CLS ' Clear screen. ' Get the filename from the command line. FileName$=COMMAND$ IF FileName$="" THEN INPUT "Enter input filename. ",FileName$ IF FileName$="" THEN END END IF OPEN FileName$ FOR INPUT AS #1 Words=0 Lines=0 Characters=0 ' Set a flag to indicate you're not looking at a ' word, then get the first character from the file. InaWord=NO DO UNTIL EOF(1) C=ASC(INPUT$(1,#1)) Characters=Characters+1 IF C=BLANK or C=CR or C=LF or C=TABC THEN ' If the character is a blank, carriage return, ' line feed, or tab, you're not in a word. IF C=CR THEN Lines=Lines+1 InaWord=NO ELSEIF InaWord=NO THEN ' The character is a printing character, ' so this is the start of a word. InaWord=YES' Count the word and set the flag. Words=Words+1 END IF LOOP PRINT Characters, Words, Lines END